package com.sunny.service;

import com.sohu.idcenter.IdWorker;
import com.sunny.dao.SunLocalDirDao;
import com.sunny.dao.SunLocalDiskDao;
import com.sunny.dao.SunLocalFileDao;
import com.sunny.entity.SunLocalDirEntity;
import com.sunny.entity.SunLocalDiskEntity;
import com.sunny.entity.SunLocalFileEntity;
import com.sunny.common.pojo.SunResult;
import com.sunny.utils.VideoUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;
import java.util.Optional;

@Service
public class DiskFileService {
    @Autowired
    private SunLocalDiskDao localDiskDao;
    @Autowired
    private SunLocalDirDao localDirDao;
    @Autowired
    private SunLocalFileDao localFileDao;
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;
    @Autowired
    private IdWorker idWorker;

    /**
     * 遍历指定目录下的所有文件
     * @param diskId 磁盘id
     * @param path  要遍历的目录
     * @return
     */
    public SunResult traverseFile(long diskId, String path, int vtype) {
        //查询磁盘信息
        Optional<SunLocalDiskEntity> optional = localDiskDao.findById(diskId);
        String diskName = "";
        if (optional.isPresent()) {
            SunLocalDiskEntity diskEntity = optional.get();
            diskName = diskEntity.getDiskName();
        }

        doTraverseFile(diskId, diskName, idWorker.getId(), new File(path), vtype, 0);

        return SunResult.ok();
    }

    private void doTraverseFile(long diskId, String diskName, long dirId, File file, int vtype, int level) {
        //判断文件是否是文件
        if (!file.isDirectory()) {
            //取文件路径
            String filePath = file.getAbsolutePath();
            //取文件名称
            String fileName = file.getName();
            //取文件大小
            long fileSize = FileUtils.sizeOf(file);
            //最后更新日期
            long mDate = file.lastModified();
            Date lastModifiedDate = new Date(mDate);
            //取视频时长
            int duration = VideoUtils.getVideoSeconds(file);
            //取文件扩展名
            String fileExt = fileName.substring(fileName.lastIndexOf(".")+1);
            //添加到文件表
            saveFileEntity(diskId, diskName, dirId, filePath, fileName, fileSize,
                    lastModifiedDate, vtype, duration, fileExt);
        } else {
            long newDirId = 0;
            if (level == 1) {
                //取文件路径
                String filePath = file.getAbsolutePath();
                //生成路径id
                newDirId = idWorker.getId();
                saveDirEntity(diskId, newDirId, filePath, "", vtype);
            } else {
                newDirId = dirId;
            }
            File[] files = file.listFiles();
            for (File f : files) {
                doTraverseFile(diskId, diskName, newDirId, f, vtype, level + 1);
            }
        }
    }


    private void saveDirEntity(long diskId, long dirId, String path, String comment, int vtype){

        taskExecutor.execute(()->{
            SunLocalDirEntity dirEntity = new SunLocalDirEntity();
            dirEntity.setDirId(dirId);
            dirEntity.setDiskId(diskId);
            dirEntity.setPath(path);
            dirEntity.setComment(comment);
            dirEntity.setType(vtype);
            localDirDao.save(dirEntity);
        });
    }

    private void saveFileEntity(long diskId, String diskName, long dirId, String dirName,
                                String fileName, long fileSize, Date modifyDate, int vtype, int duration, String fileExt) {
        taskExecutor.execute(()->{
            long id = idWorker.getId();
            SunLocalFileEntity fileEntity = new SunLocalFileEntity();
            fileEntity.setFileId(id);
            fileEntity.setFileName(fileName);
            fileEntity.setDirId(dirId);
            fileEntity.setDirName(dirName);
            fileEntity.setDiskId(diskId);
            fileEntity.setDiskName(diskName);
            fileEntity.setFileSize(fileSize);
            fileEntity.setModifyDate(modifyDate);
            fileEntity.setType(vtype);
            fileEntity.setDuration(duration);
            fileEntity.setFileExt(fileExt);
            localFileDao.save(fileEntity);
        });
    }

    @Transactional
    public SunResult addDisk(String diskName,String diskVolume, String ownUser, String comment) {
        SunLocalDiskEntity diskEntity = new SunLocalDiskEntity();
        long id = idWorker.getId();
        diskEntity.setDiskId(id);
        diskEntity.setDiskName(diskName);
        diskEntity.setDiskVolume(diskVolume);
        diskEntity.setOwnUser(ownUser);
        diskEntity.setComment(comment);
        localDiskDao.save(diskEntity);
        return SunResult.ok(id);
    }
}
